//
//  MBProgressHUD+MJ.m
//  ImageAndVideoAndRecord
//
//  Created by 李丹阳 on 2017/1/4.
//  Copyright © 2017年 李丹阳. All rights reserved.
//

#import "MBProgressHUD+MJ.h"

NSString * const HUDMsgLoading = @"正在加载...";
NSString * const HUDMsgLoadError = @"加载失败";
NSString * const HUDMsgLoadSuccessful = @"加载成功";
NSString * const HUDMsgNoMoreData = @"没有更多数据了";
NSTimeInterval   HUDHideTimeInterval = 1.2f;

@implementation MBProgressHUD (MJ)

#pragma mark 显示信息
+ (void)show:(NSString *)text icon:(NSString *)icon view:(UIView *)view
{
    if (view == nil) view = kWindow;
    if ([NSThread isMainThread]) {
        // 快速显示一个提示信息
        MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:view animated:YES];
        hud.labelText = text;
        // 设置图片
        hud.customView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:[NSString stringWithFormat:@"MBProgressHUD.bundle/%@", icon]]];
        // 再设置模式
        hud.mode = MBProgressHUDModeCustomView;
        
        // 隐藏时候从父控件中移除
        hud.removeFromSuperViewOnHide = YES;
        
        // 1秒之后再消失
        [hud hide:YES afterDelay:HUDHideTimeInterval];
    } else {
        dispatch_async(dispatch_get_main_queue(), ^{
            // 快速显示一个提示信息
            MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:view animated:YES];
            hud.labelText = text;
            // 设置图片
            hud.customView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:[NSString stringWithFormat:@"MBProgressHUD.bundle/%@", icon]]];
            // 再设置模式
            hud.mode = MBProgressHUDModeCustomView;
            
            // 隐藏时候从父控件中移除
            hud.removeFromSuperViewOnHide = YES;
            
            // 1秒之后再消失
            [hud hide:YES afterDelay:HUDHideTimeInterval];
        });
    }
}

#pragma mark 显示一些信息
+ (void)showMessage:(NSString *)message toView:(UIView *)view {
    if (view == nil) view = kWindow;
    if ([NSThread isMainThread]) {
        // 快速显示一个提示信息
        MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:view animated:YES];
        hud.detailsLabelText = message;
        // 隐藏时候从父控件中移除
        hud.removeFromSuperViewOnHide = YES;
        // YES代表需要蒙版效果
        hud.dimBackground = NO;
        
        hud.mode = MBProgressHUDModeText;
        
        // 1秒之后再消失
        [hud hide:YES afterDelay:HUDHideTimeInterval];
    } else {
        dispatch_async(dispatch_get_main_queue(), ^{
            // 快速显示一个提示信息
            MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:view animated:YES];
            hud.detailsLabelText = message;
            // 隐藏时候从父控件中移除
            hud.removeFromSuperViewOnHide = YES;
            // YES代表需要蒙版效果
            hud.dimBackground = NO;
            
            hud.mode = MBProgressHUDModeText;
            
            // 1秒之后再消失
            [hud hide:YES afterDelay:HUDHideTimeInterval];
        });
    }
}

#pragma mark 显示错误信息
+ (void)showError:(NSString *)error toView:(UIView *)view
{
    [self hideHUDForView:view];
    [self show:error icon:@"error.png" view:view];
}

#pragma mark 显示成功信息
+ (void)showSuccess:(NSString *)success toView:(UIView *)view
{
    [self hideHUDForView:view];
    [self show:success icon:@"success.png" view:view];
}

#pragma mark 显示警告信息
+ (void)showWarning:(NSString *)warning toView:(UIView *)view
{
    [self hideHUDForView:view];
    [self show:warning icon:@"warning.png" view:view];
}

+ (void)showSuccess:(NSString *)success
{
    [self hideHUDForView:nil];
    
    [self showSuccess:success toView:nil];
}

+ (void)showError:(NSString *)error
{
    [self hideHUDForView:nil];
    
    [self showError:error toView:nil];
}

+ (void)showWarning:(NSString *)warning
{
    [self hideHUDForView:nil];
    [self showWarning:warning toView:nil];
}

#pragma mark - 显示没有图片的信息
+ (void)showMessage:(NSString *)message
{
    [self hideHUDForView:nil];
    
    [self showMessage:message toView:nil];
}

#pragma mark - 显示菊花
+ (void)showProcessHud:(NSString *)msg toView:(UIView *)view {
    if (view == nil) view = kWindow;
    if ([NSThread isMainThread]) {
        MBProgressHUD *hud = [[MBProgressHUD alloc] initWithView:view];
        [view addSubview:hud];
        hud.removeFromSuperViewOnHide = YES;
        hud.mode = MBProgressHUDModeIndeterminate;
        hud.labelText = msg;
        [hud show:NO];
    } else {
        dispatch_async(dispatch_get_main_queue(), ^{
            MBProgressHUD *hud = [[MBProgressHUD alloc] initWithView:view];
            [view addSubview:hud];
            hud.removeFromSuperViewOnHide = YES;
            hud.mode = MBProgressHUDModeIndeterminate;
            hud.labelText = msg;
            [hud show:NO];
        });
    }
}

#pragma mark - 隐藏 hud
+ (void)hideHUDForView:(UIView *)view
{
    if (view == nil) view = kWindow;
    if ([NSThread isMainThread]) {
        [self hideHUDForView:view animated:YES];
    } else {
        dispatch_async(dispatch_get_main_queue(), ^{
            [self hideHUDForView:view animated:YES];
        });
    }
}

+ (void)hideHUD
{
    [self hideHUDForView:nil];
}

@end
